window.onload = function ()
{
    InitTimeDisplay();
    let tabName = GetParams("tab");
    if(tabName != null)
    {
        let button = document.getElementById(tabName + "-button");
        if (button != null)
            button.click();
        else
            document.getElementsByClassName("tab-button")[0].click();
    }
    else
    {
        document.getElementsByClassName("tab-button")[0].click();
    }
    InitServerStatus()
}


function ChangeBox(event, boxId)
{
    let tabBoxes = document.getElementsByClassName("tab-box");
    for(let i = 0; i < tabBoxes.length; i++)
        tabBoxes[i].style.display = "none";
    let tabButtons = document.getElementsByClassName("tab-button-active");
    for(let i = 0; i < tabButtons.length; i++)
        tabButtons[i].className = tabButtons[i].className.replace("-active", "");
    document.getElementById(boxId).style.display = "block";
    event.currentTarget.className += "-active";
}

function Search()
{
    let searchBox = event.currentTarget.parentNode;
    let tableName = searchBox.getElementsByClassName("table-name-input")[0].value;
    let keyword = searchBox.getElementsByClassName("search-text-input")[0].value;
    if (tableName === "") {
        ShowAlertOverlay("表名不能为空.");
        return
    }
    if (keyword === "") {
        ShowAlertOverlay("搜索关键字不能为空.");
        return;
    }
    window.location.href = "searchResult.html?tableName=" + tableName + "&keyword=" + keyword;
}

function InitServerStatus()
{
    let hdfsText = document.getElementById("hdfs-status");
    let hbaseText = document.getElementById("hbase-status");
    let esText = document.getElementById("es-status");
    let url = baseUrl + "/manage/getStatus";
    let http = new XMLHttpRequest();
    http.open("GET", url);
    http.send();
    http.onreadystatechange = function ()
    {
        if(this.readyState === http.DONE)
        {
            let resJson = JSON.parse(this.responseText);
            let displayText = "";
            switch (this.status)
            {
                case 200:
                    if (!resJson.Hdfs) {
                        hdfsText.innerText = "连接状况：异常"
                        hdfsText.style.color = "red";
                    }
                    else {
                        hdfsText.innerText = "连接状况：正常"
                        hdfsText.style.color = "black";
                    }

                    if (!resJson.HBase) {
                        hbaseText.innerText = "连接状况：异常"
                        hbaseText.style.color = "red";
                    }
                    else {
                        hbaseText.innerText = "连接状况：正常"
                        hbaseText.style.color = "black";
                    }

                    if (!resJson.Es) {
                        esText.innerText = "连接状况：异常"
                        esText.style.color = "red";
                    }
                    else {
                        esText.innerText = "连接状况：正常"
                        esText.style.color = "black";
                    }
                    break;
                default:
                    hdfsText.innerText = "连接状况：异常"
                    hdfsText.style.color = "red";
                    hbaseText.innerText = "连接状况：异常"
                    hbaseText.style.color = "red";
                    esText.innerText = "连接状况：异常"
                    esText.style.color = "red";
                    break;
            }
        }
    };
}